<?php
namespace app\admin\model;

use think\Model;

/**
 * 栏目模型
 * @package app\admin\model
 */
class GwCate extends Model
{

    public static function getList($request)
    {
        // $data = self::alias('c')

        $data = self::alias('c')
            ->field('c.*,gt.name as type_name,tem.name as template_name')
            ->leftjoin('gw_type gt', 'gt.id = c.type_id')
            ->leftjoin('gw_template tem', 'tem.id = c.template_id')
            ->paginate($request['limit'], false, ['query' => $request])
            ->each(function ($item, $key) {
                switch ($item->status) {
                    case '0':
                        $item->status = '<span class="layui-badge status-jy">已禁用</span>';
                        break;
                    case '1':
                        $item->status = '<span class="layui-badge layui-bg-green status-qy">已启用</span>';
                        break;
                    default:
                        $item->status = '<span class="layui-badge layui-bg-orange status-jy">未知</span>';
                        break;
                }
                switch ($item->template_name) {
                    case '':
                        $item->template_name = '未配置';
                        break;
                    default:
                        break;
                }

            });

        return $data->toarray();
    }

    /**
     * 检测当前权限是否存在子级权限
     * @param  integer $authId 待查询的权限主键ID
     * @return boolean         true代表存在子级 false代表没有子级
     */
    public function checkChild($authId)
    {
        $where = [
            'auth_pid' => $authId,
        ];
        return $this::where($where)->find(); // 如果有一个都不能删除
    }

    /**
     * 获取当前权限的子级权限主键ID
     * @param  integer $authId 当前权限主键
     * @return array         一维数组
     */
    public function getChild($authId)
    {
        $data = $this::order('order_num , auth_id ')->select()->toArray();

        // return $data;
        return $this->_getChild($data, $authId);
    }

    public function _getChild($data, $authId)
    {
        static $list = array();
        foreach ($data as $k => $v) {

            if ($v['auth_pid'] == $authId) {
                $list[] = $v['auth_id'];
                $this->_getChild($data, $v['auth_id']);
            }

        }
        return $list;
    }

    /**
     * 获取层级关系数据
     * @return array 层级关系数据
     */
    public static function getTree()
    {
        // 使用递归取出数据
        $data = self::select()->toArray();
        // 递归
        return self::_getTree($data);

    }
    /**
     * 递归处理数据
     * @param  array  $data  待处理的数据
     * @param  integer $pid   上级权限ID
     * @param  integer $level 层级
     * @return array         拥有层级关系的数据
     */
    public static function _getTree($data, $pid = 0, $level = 0)
    {
        // 递归处理数据
        static $list = array();
        foreach ($data as $k => $v) {
            if ($v['parentid'] == $pid) {
                $v['level'] = $level;
                $list[]     = $v;
                self::_getTree($data, $v['id'], $level + 1);
            }
        }
        return $list;
    }

    public static function add($data)
    {
        try {
            self::create($data);
            return ['code' => '200', 'msg' => '新增成功'];
        } catch (\Exception $e) {
            trace('新增导航栏信息失败：' . $e->getMessage());
            return ['code' => '422', 'msg' => '新增失败'];
        }
    }

    public static function upd($data)
    {
        try {
            self::where('id', $data['id'])->update($data);
            return ['code' => '200', 'msg' => '更新成功'];
        } catch (\Exception $e) {
            trace('编辑导航栏信息错误：' . $e->getMessage(), 'info');
            $data = [
                'code' => '422',
                'msg'  => '编辑失败',
            ];
            if (preg_match('/for key/', $e->getMessage())) {
                $data['msg'] = '请检查栏目标识是否重复';
            }
            return $data;
        }
    }
}
